<template>
  <div>
    <h1>用户列表</h1>
    <el-form
      :inline="true"
      :model="model"
      class="demo-form-inline"
      label-width="80px"
      @submit.native.prevent="onSubmit"
    >
      <el-form-item label="用户名:">
        <el-input v-model="model.name" placeholder="请输入用户名" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号:">
        <el-input v-model="model.phone" type="tel" placeholder="请输入手机号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="dialogFormVisible = true">新增用户</el-button>
    <el-dialog title="新增用户" :visible.sync="dialogFormVisible" center @open="open">
      <el-form :model="user" ref="form" @submit.native.prevent="addUser">
        <el-form-item label="用户名" label-width="80px">
          <el-input v-model="user.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" label-width="80px">
          <el-input type="tel" v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="80px">
          <el-input v-model="user.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </div>
    </el-dialog>
    <el-table :data="userList">
      <el-table-column prop="_id" label="ID" width="240"></el-table-column>
      <el-table-column prop="name" label="用户名"></el-table-column>
      <el-table-column prop="phone" label="电话"></el-table-column>
      <el-table-column prop="createdAt" label="创建时间"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            @click="$router.push(`/user/edit/${scope.row._id}`)"
            type="text"
            size="small"
          >查看</el-button>
          <el-button @click="remove(scope.row._id, scope.row.name)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      model: {},
      dialogFormVisible: false,
      user: {},
      userList: [],
    };
  },
  methods: {
    async fetch() {
      const res = await this.$http.get("rest/users");
      this.userList = res.data;
    },
    async addUser() {
      await this.$http.post("rest/users", this.user);
      this.dialogFormVisible = false;
      this.fetch();
    },
    async onSubmit() {
      let { name, phone } = this.model;
      if (phone == "") {
        delete this.model["phone"];
      }
      if (name == "") {
        delete this.model["name"];
      }
      if (name == "" && phone == "") {
        const res = this.fetch();
        this.userList = res.data;
      } else {
        const res = await this.$http.post("rest/users/select", this.model);
        this.userList = res.data;
      }
    },
    open() {
      this.user = {};
    },
    async remove(id, name) {
      this.$confirm(`是否删除："${name}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await this.$http.delete(`rest/users/${id}`).then(() => {
            this.$message({
              message: "删除成功",
              type: "success"
            });
            this.fetch();
          });
        })
        .catch(() => {});
    },
  },
  created() {
    this.fetch();
  }
};
</script>